package com.moqianfan.mcuassistant.activity;

import android.annotation.SuppressLint;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.TextView;

import com.moqianfan.mcuassistant.R;
import com.moqianfan.mcuassistant.adapter.CommDataAdapter;
import com.moqianfan.mcuassistant.utils.FormatUtil;
import com.moqianfan.mcuassistant.utils.LogUtil;
import com.moqianfan.mcuassistant.view.LineGridView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by limh on 2016/11/24.
 * PCB单位换算
 */

public class PCBTransActivity extends BaseActivity implements AdapterView.OnItemClickListener, TextWatcher {
    @BindView(R.id.grad_key)
    LineGridView gridKey;
    @BindView(R.id.edit_up)
    EditText editUp;
    @BindView(R.id.edit_down)
    EditText editDown;
    @BindView(R.id.txt_up)
    TextView txtUp;
    @BindView(R.id.txt_down)
    TextView txtDown;
    @BindView(R.id.txt_change)
    TextView txtChange;

    private ArrayList<String> data;
    private int flag = 0;

    @Override
    public void findViews() {
        setContentView(R.layout.activity_pcb);
    }

    public void initViews() {
        data = new ArrayList<>();
        data.add("1");
        data.add("2");
        data.add("3");
        data.add("mm");
        data.add("4");
        data.add("5");
        data.add("6");
        data.add("mil");
        data.add("7");
        data.add("8");
        data.add("9");
        data.add("退格");
        data.add("00");
        data.add("0");
        data.add(".");
        data.add("清空");
        gridKey.setAdapter(new CommDataAdapter<String>(data, R.layout.view_item_key) {
            @Override
            public void bindView(ViewHolder holder, String obj) {
                holder.setText(R.id.txt_key, obj);
            }
        });

        gridKey.setOnItemClickListener(this);
        editUp.addTextChangedListener(this);
    }

    @Override
    public void initDatas() {

    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (data.get(position)) {
            case "mm":
                txtUp.setText("mm（毫米）");
                txtDown.setText("mil（密耳）");
                txtChange.setText("转换：mm-->mil");
                flag = 0;
                break;
            case "mil":
                txtUp.setText("mil（密耳）");
                txtDown.setText("mm（毫米）");
                txtChange.setText("转换：mil-->mm");
                flag = 1;
                break;
            case "清空":
                editDown.setText("");
                editUp.setText("");
                break;
            case "退格":
                if (!TextUtils.isEmpty(editUp.getText().toString())) {
                    editUp.setText(editUp.getText().toString().substring(0, editUp.getText().toString().length() - 1));
                }
                break;
            case ".":
                if (!editUp.getText().toString().contains("."))
                    editUp.append(".");
                break;
            default:
                editUp.append(data.get(position));
                break;

        }
    }

    @OnClick(R.id.image_close)
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.image_close:
                finish();
                break;
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (!TextUtils.isEmpty(s.toString()) && !".".equals(s.toString())) {
            float upText = Float.valueOf(s.toString());
            float result;
            LogUtil.w("PCB", "输入值" + upText);
            switch (flag) {
                case 0://mm-->mil
                    result = (float) (100 / 2.54 * upText);
                    editDown.setText(FormatUtil.formatThree(result));
                    break;
                case 1://mil-->mm
                    result = (float) (2.54 / 100 * upText);
                    editDown.setText(FormatUtil.formatTwo(result));
                    break;
            }
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
}
